package com.cloudsiteservice8004.service.impl;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudsiteservice8004.entity.SiteInfo;
import com.cloudsiteservice8004.mapper.SiteInfoMapper;
import com.cloudsiteservice8004.service.SiteInfoService;
import com.cloudsiteservice8004.util.SnowflakeIdGenerator;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

/**
* @author 26407
* @description 针对表【site_info】的数据库操作Service实现
* @createDate 2025-06-11 11:32:10
*/
@Service
public class SiteInfoServiceImpl extends ServiceImpl<SiteInfoMapper, SiteInfo>
    implements SiteInfoService {
    @Override
    public IPage<SiteInfo> getServiceInfo(Integer pageNum, Integer pageSize, String siteCode) {

        LambdaQueryWrapper<SiteInfo> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(siteCode)){
            wrapper.eq(SiteInfo::getSiteCode, siteCode);
            return this.page(new Page<>(pageNum, pageSize), wrapper);
        }
        return this.page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public Boolean create(SiteInfo siteInfo) {

        Preconditions.checkArgument(siteInfo.getSiteStatus() != null, "站点状态不能为空");
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(1,1);
        siteInfo.setSiteCode(snowflakeIdGenerator.nextId().toString());
        return save(siteInfo);
    }

    @Override
    public Boolean updates(SiteInfo siteInfo) {
        if (siteInfo.getId() == null) {
            return false;
        }
        return updateById(siteInfo);
    }

    @Override
    public boolean removeByCode(SiteInfo siteInfo) {
        if(siteInfo.getId() != null){
            return removeById(siteInfo.getId());
        }
        if(siteInfo.getSiteCode() != null){
            return remove(new QueryWrapper<SiteInfo>().eq("site_code", siteInfo.getSiteCode()));
        }
        return false;
    }
}




